// 发送验证码
import { ref } from "vue";
import http from "@/api/http.js";
import { ElMessage } from "element-plus";

export const useLoginHook = () => {
  const count = ref(0);
  const countLoading = ref(false);
  const setCountDown = (i = 60, mobile) => {
    countLoading.value = false;
    if (i > 0) {
      if (!mobile) {
        ElMessage.warning("请输入手机号");
        return;
      }
      countLoading.value = true;
      http
        .get({
          url: "/sms/mobile",
          params: {
            lang: "zh",
            mobile,
          },
        })
        .then(
          () => {
            count.value = i;
            countLoading.value = false;
            const time = setInterval(() => {
              if (count.value <= 0) {
                clearInterval(time);
              }
              count.value--;
            }, 1000);
          },
          () => {
            count.value = 0;
            countLoading.value = false;
          }
        );
    } else {
      count.value = i;
    }
  };
  return {
    count,
    countLoading,
    setCountDown,
  };
};
